package routers

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
	_ "repairs/docs"

	"repairs/pkg/export"
	"repairs/pkg/qrcode"
	"repairs/pkg/upload"
	"repairs/routers/api"
	"repairs/routers/api/v1"
)

// 生成路由
// InitRouter initialize routing information
func InitRouter() *gin.Engine {
	r := gin.New()
	r.Use(gin.Logger())
	r.Use(gin.Recovery())

	r.StaticFS("/export", http.Dir(export.GetExcelFullPath()))
	r.StaticFS("/upload/images", http.Dir(upload.GetImageFullPath()))
	r.StaticFS("/qrcode", http.Dir(qrcode.GetQrCodeFullPath()))

	r.POST("/api/v1/token", api.Token)
	r.POST("/api/v1/login", api.Login)
	//r.POST("/api/v1/registration", api.Registration)
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	r.POST("/upload", api.UploadImage)

	apiV1 := r.Group("/api/v1")
	//apiV1.Use(jwt.JWT())
	{
		//获取标签列表
		apiV1.GET("/tags", v1.GetTags)
		//新建标签
		apiV1.POST("/tags", v1.AddTag)
		//更新指定标签
		apiV1.PUT("/tags/:id", v1.EditTag)
		//删除指定标签
		apiV1.DELETE("/tags/:id", v1.DeleteTag)
		//导出标签
		r.POST("/tags/export", v1.ExportTag)
		//导入标签
		r.POST("/tags/import", v1.ImportTag)

		//获取文章列表
		apiV1.GET("/articles", v1.GetArticles)
		//获取指定文章
		apiV1.GET("/articles/:id", v1.GetArticle)
		//新建文章
		apiV1.POST("/articles", v1.AddArticle)
		//更新指定文章
		apiV1.PUT("/articles/:id", v1.EditArticle)
		//删除指定文章
		apiV1.DELETE("/articles/:id", v1.DeleteArticle)
		//生成文章海报
		apiV1.POST("/articles/poster/generate", v1.GenerateArticlePoster)

		// _method : get => GetMalfunction add => AddMalfunction
		apiV1.POST("/malfunctions", v1.MalFunctions)
		//获取故障列表
		//apiV1.POST("/malfunctions", v1.GetMalfunctions)
		//新建故障
		//apiV1.POST("/malfunctions", v1.AddMalfunction)
		//更新故障
		apiV1.PUT("/malfunctions/:id", v1.EditAMalfunction)
		//删除故障
		apiV1.DELETE("/malfunctions/:id", v1.DeleteMalfunction)

		//获取租借列表
		apiV1.GET("/rents", v1.GetRents)
		//获取用户对应的租借记录
		apiV1.POST("/user_rent", v1.GetUserRent)
		//增加租借
		apiV1.POST("/rents", v1.AddRent)
		//删除租借
		apiV1.DELETE("/rents/:id", v1.DeleteRent)

		// _method: get => GetRooms ,  add => AddRoom
		apiV1.POST("/rooms", v1.Rooms)
		//获取房间列表
		//apiV1.POST("/rooms", v1.GetRooms)
		//删除房间
		apiV1.DELETE("/rooms/:id", v1.DeleteRoom)
		//添加房间
		//apiV1.POST("/rooms", v1.AddRoom)
	}

	r.GET("/api/v1/ping", func(c *gin.Context) {
		c.JSON(200, "pong")
	})
	return r
}
